# Mattise Makefile
# Copyright 2007 Matthew Iselin

# final binary location
FINALBIN:=kernel.bin

# install
install: $(FINALBIN)
	@$(ECHO) $(SRCS) > srcs.txt
	cp $(FINALBIN) "Z:/boot/kernel.bin"
	cp $(FINALBIN) "B:/boot/kernel.bin"

# compiler and flags
CXX:=/usr/cross/bin/i586-elf-g++.exe
CXXFLAGS:=-I ./include -I ./lib -nostdlib -fno-rtti -fno-exceptions -O2

# linker and flags
LD:=/usr/cross/bin/i586-elf-ld.exe
LDFLAGS:=-T ./linker.ld -Map link.map -o

# project directories
PROJDIRS:=$(shell find ./ -type d -not -name "CVS")

# assembler
AS:=/usr/cross/bin/i586-elf-as.exe
AS_SRCS:=$(shell find $(PROJDIRS) -maxdepth 1 -name "*.asm" -type f)

# source and objects, and final binary
SRCS:=$(shell find $(PROJDIRS) -maxdepth 1 -name "*.cc" -type f)
OBJS:=$(patsubst %.asm,%.o,$(AS_SRCS)) $(patsubst %.cc,%.o,$(SRCS))

# echo command location
ECHO:=echo

# dependiencies
DEPFILES:=$(patsubst %.cc,%.d,$(SRCS))
ASDEPFILES:=$(patsubst %.asm,%.d,$(AS_SRCS))

# tests the makefile
test:
	$(ECHO) $(DEPFILES)

# clean up
clean:
	rm $(OBJS) $(DEPFILES) *.bin

# buld the full kernel
all: $(FINALBIN)

# link command
$(FINALBIN) : $(OBJS)
	@$(ECHO) Linking
	@$(ECHO) $(OBJS) > objects.txt
	@$(LD) $(LDFLAGS) $(FINALBIN) @objects.txt
	@rm comp.txt objects.txt

# included files
-include $(DEPFILES)

# C++ source
%.o : %.cc
	@$(ECHO) Compiling $<
	@$(ECHO) $(CXXFLAGS) -MMD -MP -MT "$*.d" -c $< -o $@ > comp.txt
	@$(CXX) $(CXXFLAGS) @comp.txt

# assembly
%.o : %.asm isr.inc irq.inc
	@$(ECHO) Assembling $<
	@$(AS) $< -o $@
